# _*_ coding: utf-8 _*_
# .@FileName:gen_name_exp_dialog
# .@Data....:2025-01-30 : 21 : 19
# .@Aurhor..:LiuJingYu
"""
launch:
   import maya.cmds as cmds
   FileName.main() 
"""

from __future__ import unicode_literals, print_function
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from typing import List, Tuple

import gen_name_exp

import setting

# class GenNameDialog(QDialog):
#     def __init__(self, parent=None):
#         # 调用父类QDialog的构造函数，确保父类的初始化逻辑被执行
#         super(GenNameDialog, self).__init__(parent)
#         # 设置对话框的窗口标题为 "Gen Name"
#         self.setWindowTitle("Gen Name")
#         self.main_layout = QHBoxLayout(self)
#
#
#         self.node_size = QSpinBox()
#         self.node_size.setRange(5, 999)
#
#         self.exp_line_edit = QLineEdit()
#         self.type_line_edit = QLineEdit()
#
#         self.gen_bn = QPushButton("生成")
#         self.gen_bn.clicked.connect(self.gen)
#
#         self.main_layout.addWidget(QLabel("节点计数： "))
#         self.main_layout.addWidget(self.node_size)
#
#         self.main_layout.addWidget(QLabel("表达式： "))
#         self.main_layout.addWidget(self.exp_line_edit)
#
#
#         self.main_layout.addWidget(QLabel("节点类型： "))
#         self.main_layout.addWidget(self.type_line_edit)
#
#         self.main_layout.addWidget(self.gen_bn)
#
#         self.setting_font()
#
#     def setting_font(self):
#         font_size = setting.get("font_size", 7)
#         f = self.font()
#         f.setPointSize(font_size)
#         self.setFont(f)
#
#     def gen(self):
#         node_size = self.node_size.value()
#         exp = self.exp_line_edit.text()
#         type_ = self.type_line_edit.text()
#         values = [{"name_id": i} for i in range(node_size)]
#
#         self.names = [(n, type_) for n in gen_name_exp.compile_template(exp, values)]
#
#         self.close()
#
#
#
#
# def exec_(parent=None):
#     dia = GenNameDialog(parent)
#     dia.exec_()
#     return dia.names



class GenNameDialog(QDialog):
    """名称生成对话框 (兼容Python 3.7)

    用于生成指定格式的节点名称，包含以下功能：
    - 设置生成节点数量
    - 输入名称生成表达式
    - 指定节点类型
    - 生成名称列表

    Attributes:
        main_layout: 主布局管理器
        node_size: 节点数量输入框
        exp_line_edit: 表达式输入框
        type_line_edit: 类型输入框
        gen_bn: 生成按钮
        names: 生成的结果列表
    """

    def __init__(self, parent=None):
        """初始化对话框组件

        Args:
            parent: 父级窗口组件，默认为None
        """
        # 显式调用父类初始化方法（兼容Python 3.7的super写法）
        super(GenNameDialog, self).__init__(parent)

        # 窗口基本设置
        self.setWindowTitle("Gen Name")
        self.names = []  # 初始化结果列表

        # 初始化布局和组件
        self._init_ui()
        self._init_style()

    def _init_ui(self):
        """初始化用户界面组件"""
        # 主布局使用水平布局
        self.main_layout = QHBoxLayout(self)

        # 节点数量输入框
        self.node_size = QSpinBox()
        self.node_size.setRange(5, 999)  # 设置数值范围
        self.node_size.setValue(10)  # 设置默认值

        # 表达式输入框
        self.exp_line_edit = QLineEdit()
        self.exp_line_edit.setPlaceholderText("输入名称表达式")

        # 类型输入框
        self.type_line_edit = QLineEdit()
        self.type_line_edit.setPlaceholderText("输入节点类型")

        # 生成按钮
        self.gen_bn = QPushButton("生成")
        self.gen_bn.clicked.connect(self.gen)  # 连接点击信号

        # 将组件添加到布局
        self._arrange_components()

    def _arrange_components(self):
        """排列界面组件"""
        # 按顺序添加组件到水平布局
        self.main_layout.addWidget(QLabel("节点计数："))
        self.main_layout.addWidget(self.node_size)
        self.main_layout.addWidget(QLabel("表达式："))
        self.main_layout.addWidget(self.exp_line_edit)
        self.main_layout.addWidget(QLabel("节点类型："))
        self.main_layout.addWidget(self.type_line_edit)
        self.main_layout.addWidget(self.gen_bn)

    def _init_style(self):
        """初始化界面样式"""
        self.setMinimumWidth(600)  # 设置最小窗口宽度
        self.setting_font()

    def setting_font(self):
        fs = setting.get("font", None)
        if fs is None:
            fs =  QFont().toString()
        f = QFont()
        f.fromString(fs)
        self.setFont(f)

    def gen(self):
        """执行名称生成操作"""
        # 获取用户输入值
        node_count = self.node_size.value()
        expression = self.exp_line_edit.text().strip()
        node_type = self.type_line_edit.text().strip()

        # 生成测试数据（假设每个节点需要name_id参数）
        sample_data = [{"name_id": i} for i in range(node_count)]

        # 调用名称生成器（假设gen_name_exp.compile返回名称列表）
        try:
            generated_names = gen_name_exp.compile_template(expression, sample_data)
            # 组合名称和类型 (name, type)
            self.names = [(name, node_type) for name in generated_names]
        except Exception as e:
            # 处理生成异常（实际应使用更具体的异常类型）
            self.names = []
            print(f"名称生成失败: {str(e)}")

        self.close()  # 关闭对话框


def exec_(parent=None) -> List[Tuple[str, str]]:
    """创建并执行名称生成对话框 (兼容旧式exec_命名)

    Args:
        parent: 父级窗口组件，默认为None

    Returns:
        List[Tuple[str, str]]: 生成的结果列表，格式为[(名称, 类型), ...]
    """
    dialog = GenNameDialog(parent)
    dialog.exec_()  # 显示模态对话框
    return dialog.names


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    exec_()
    sys.exit(app.exec_())
